/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hybris;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;

/**
 *
 * @author menderleit
 */
public class Part1 extends DemoPart {

    private float fadeAlpha;
    private int fadeState;
    private int bgSegs;
    private int bgSegW;
    private double bgAng;
    private Object3D o3d;
    private double oAngX, oAngY;
    private Random rnd;
    private double objAlpha;

    public Part1(FullScreen fs) {
        super(fs);

        rnd = new Random();

        fadeAlpha = 1.0f;
        fadeState = 0;
        bgSegs = 128;
        bgSegW = fs.getWidth() / bgSegs;
        bgAng = 0.0;

        o3d = new Object3D(6);
        Vector3[] vectors = {
            // BACK
            new Vector3(-10, 10, 10),
            new Vector3(-10, -10, 10),
            new Vector3(10, -10, 10),
            new Vector3(10, 10, 10),
            // FRONT
            new Vector3(-10, 10, -10),
            new Vector3(10, 10, -10),
            new Vector3(10, -10, -10),
            new Vector3(-10, -10, -10),
            // RIGHT
            new Vector3(10, 10, -10),
            new Vector3(10, 10, 10),
            new Vector3(10, -10, 10),
            new Vector3(10, -10, -10),
            // LEFT
            new Vector3(-10, 10, 10),
            new Vector3(-10, 10, -10),
            new Vector3(-10, -10, -10),
            new Vector3(-10, -10, 10),
            // TOP
            new Vector3(-10, 10, 10),
            new Vector3(10, 10, 10),
            new Vector3(10, 10, -10),
            new Vector3(-10, 10, -10),
            // BOTTOM
            new Vector3(-10, -10, -10),
            new Vector3(10, -10, -10),
            new Vector3(10, -10, 10),
            new Vector3(-10, -10, 10)
        };
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 4; j++) {
                o3d.setFace(i, j, vectors[i * 4 + j]);
            }
            o3d.setFaceColor(i, new Color(255, 255, 255, 0));
        }

        oAngX = 0.0;
        oAngY = 0.0;
        objAlpha = 0.0;
    }

    @Override
    public void update(long difTime) {
        updateTime(difTime);
        switch (fadeState) {
            case 0:
                fadeState = 1;
                break;
            case 1:
                fadeAlpha -= 0.0005f * difTime;
                if (fadeAlpha <= 0.0f) {
                    fadeAlpha = 0.0f;
                    fadeState = 3;
                    nextStage();
                }
                break;
            case 2:
                fadeAlpha += 0.0005f * difTime;
                if (fadeAlpha >= 1.0f) {
                    fadeAlpha = 1.0f;
                    stop();
                }
                break;
            default:
                break;
        }
        bgAng += 0.125 * difTime;
        bgAng = wrapAngle(bgAng);
        oAngX += 0.05 * difTime;
        oAngX = wrapAngle(oAngX);
        oAngY += 0.03333 * difTime;
        oAngY = wrapAngle(oAngY);
        if (getStage() == 1) {
            objAlpha += 0.0005 * difTime;
            if (objAlpha >= 0.75) {
                objAlpha = 0.75;
                nextStage();
            }
        }
        if (getStage() == 2) {
            if (getTime() >= 20000) {
                nextStage();
            }
        }
        if (getStage() == 3) {
            objAlpha -= 0.0005 * difTime;
            if (objAlpha <= 0.0) {
                objAlpha = 0.0;
                nextStage();
            }
        }
        if (getStage() == 4) {
            fadeState = 2;
        }
    }

    @Override
    public void draw() {
        FullScreen fs = getFullScreen();
        double FOV = (double) ((double) fs.getWidth() / (double) fs.getHeight());
        Graphics2D g = (Graphics2D) fs.getGraphics();

        // Clear screen.
        g.setColor(new Color(0, 0, 0));
        g.fillRect(0, 0, fs.getWidth(), fs.getHeight());

        for (int i = 0; i < bgSegs; i++) {
            double ang = bgAng + (180.0 / bgSegs) * i;
            double offset = Math.sin(Math.toRadians(ang)) * 50.0;
            g.setColor(new Color(31, 63, 127));
            g.fillRect(i * bgSegW, fs.getHeight2() - 250 + (int) offset, bgSegW, 500);
            g.setColor(new Color(63, 95, 159));
            g.fillRect(i * bgSegW, fs.getHeight2() - 255 + (int) offset, bgSegW, 5);
            g.fillRect(i * bgSegW, fs.getHeight2() + 250 + (int) offset, bgSegW, 5);
            g.setColor(new Color(95, 127, 191));
            g.fillRect(i * bgSegW, fs.getHeight2() - 260 + (int) offset, bgSegW, 5);
            g.fillRect(i * bgSegW, fs.getHeight2() + 255 + (int) offset, bgSegW, 5);
            g.setColor(new Color(159, 191, 255));
            g.fillRect(i * bgSegW, fs.getHeight2() - 265 + (int) offset, bgSegW, 5);
            g.fillRect(i * bgSegW, fs.getHeight2() + 260 + (int) offset, bgSegW, 5);
        }

        Vector3 rot = new Vector3(oAngX, oAngY, 0);
        o3d.setRotation(rot);
        o3d.setPosition(new Vector3(0, 0, 35));
        for (int i = 0; i < 6; i++) {
            double a = 255.0 * objAlpha;
            o3d.setFaceColor(i, new Color(255, 255, 255, (int) a));
        }
        o3d.draw(g, fs.getWidth(), fs.getHeight(), FOV);

        if (fadeState < 3) {
            g.setColor(new Color(0.0f, 0.0f, 0.0f, fadeAlpha));
            g.fillRect(0, 0, fs.getWidth(), fs.getHeight());
        }

//        g.setColor(Color.yellow);
//        g.drawString("FPS: "+fs.getFPS(), 0, 10);

        fs.show();
        g.dispose();
    }

    public double wrapAngle(double angle) {
        double newAngle = angle;
        if (angle < 0.0) {
            newAngle += 360.0;
        }
        if (angle >= 360.0) {
            newAngle -= 360.0;
        }
        return newAngle;
    }
}
